using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DocLibApp
{
    /// <summary>
    /// intend to be a prompt for user to select something from database listing generate by query, then pass
    /// the selection back to calling program
    /// </summary>
    public partial class FormDatabaseSelect : FormBase
    {
        private string IDSelected = "";

        public FormDatabaseSelect()
        {
            InitializeComponent();
        }

        public string Title
        {
            set
            {
                this.Text = value;
            }
        }

        public string SelectedID
        {
            get
            {
                return this.IDSelected;
            }
        }

        public void OpenSelection(System.Collections.ArrayList aDataList, string key, string tablename)
        {
            lsv_data.ShowData(aDataList, key, tablename);
        }

        public void OpenSelection(System.Collections.ArrayList aDataList, string Key)
        {
            lsv_data.ShowData(aDataList, Key);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            MakeSelection();
        }

        private void MakeSelection()
        {
            if (lsv_data.SelectedItems.Count > 0)
            {
                ListViewItem aitem = lsv_data.SelectedItems[0];
                if (aitem.Tag != null && !string.IsNullOrEmpty(aitem.Tag.ToString()))
                {
                    this.IDSelected = aitem.Tag.ToString();
                    this.Close();
                }
            }
        }

        private void lsv_data_DoubleClick(object sender, EventArgs e)
        {
            MakeSelection();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.IDSelected = "";
            this.Close();
        }
    }
}